package com.stone.business.jc.action;

/**
 * 绩效考核细则字典
 * <pre>
 * @author：JZ
 * @since:2010/02/10
 * </pre>
 */
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.sys.pojo.TJxkhxz;
import com.stone.sys.service.JxkhxzService;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class JxkhxzAction extends BaseAction {

	private JxkhxzService jxkhxzService;
	
	// 实体用于修改
	private TJxkhxz entity;
	
	// 父节点
	private String parentid;
	
	// 删除时用标示
	private String id;

	/**
	 * 查询用户菜单
	 * @return
	 * @throws AppException
	 */	
	@SuppressWarnings("unchecked")
	public void getData() throws IOException  {
		
		List<HashMap<String,Object>> listTree;

		listTree = jxkhxzService.getListByDHql(
				"t.id as id, t.mkname || t.kpxm || kpnr as text,  t.jjfz as qtip,t.parentid as parentid, (case when (select count(o.id) from TJxkhxz o where o.parentid = t.id)>0 then false else true end) as leaf", 
				"parentid = ? ", 
				new Object[]{Long.parseLong(parentid)}, 
				" order by t.id", 
				" TJxkhxz t");
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
        
        super.getResponse().getWriter().print(json.toString());
		
	}
	
	/**
	 * 新建或修改一条记录
	 * @return
	 * @throws AppException
	 */	
	public void save() throws IOException  {
		
		this.jxkhxzService.save(this.entity);
		
		super.getResponse().getWriter().print("{success:true}");
	}

	/**
	 * 删除一条记录
	 * @return
	 * @throws AppException
	 */	
	public void delete() throws IOException  {
		this.jxkhxzService.delete(id, TJxkhxz.class.getName());
		super.getResponse().getWriter().print("{success:1}");
	}

	
	/**
	 * 查询一条记录
	 * @return
	 * @throws AppException
	 */	
	public void find()  throws IOException  {
		
		this.entity = (TJxkhxz) jxkhxzService.findById(Long.parseLong(id), TJxkhxz.class.getName());
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON((this.entity != null ? this.entity : new TJxkhxz()), jsonConfig);

        super.getResponse().getWriter().print(json.toString());
	}


	public void setJxkhxzService(JxkhxzService jxkhxzService) {
		this.jxkhxzService = jxkhxzService;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
	}

	public void setEntity(TJxkhxz entity) {
		this.entity = entity;
	}

	public TJxkhxz getEntity() {
		return entity;
	}
	
	

}
